package br.com.marcelo.reservasis.client.componentes;

import br.com.aroeirasoftware.gwtcomponents.client.components.ASCommandBar;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASLayout;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASMenuBar;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASOrientation;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASPage;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASStatusPanel;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASTabBarPage;
import br.com.marcelo.reservasis.client.pagina.login.LoginPagina;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import java.util.List;

/**
 *
 * @author Marcelo
 */
public class ReservaSISLayout extends Composite implements ASLayout {

    private ASStatusPanel status;
    private ASTabBarPage tabbar;
    private ASCommandBar barraComandos;
    private SimplePanel containerBarraMenu;
    private SimplePanel containerBarraComandos;
    private SimplePanel containerTabbar;
    private DockPanel layout;
    private ScrollPanel conteudo;
    private VerticalPanel topo;
    private static final String STYLE_NAME_BARRA_COMANDOS = "as-layout-barra-comandos";
    private static final int SCROLL_DIFF = 230;

    public ReservaSISLayout() {
        initComponents();
    }

    private void initComponents() {
        Window.enableScrolling(false);
        Window.setMargin("0px");

        conteudo = new ScrollPanel();
        conteudo.setWidth("100%");
        conteudo.clear();
        conteudo.add(new LoginPagina(null, ReservaSISApplication.getInstance(), ASOrientation.HORIZONTAL));

        status = new ASStatusPanel();

        tabbar = new ASTabBarPage(conteudo);

        containerTabbar = new SimplePanel();
        containerTabbar.setHeight("26px");
        containerTabbar.setWidget(tabbar);

        barraComandos = new ASCommandBar();

        containerBarraComandos = new SimplePanel();
        containerBarraComandos.setStyleName(STYLE_NAME_BARRA_COMANDOS);
        containerBarraComandos.setWidget(barraComandos);
        containerBarraComandos.setVisible(false);

        containerBarraMenu = new SimplePanel();
        containerBarraMenu.setVisible(false);
        containerBarraMenu.setWidget(new ReservaSISBarraMenu());

        topo = new VerticalPanel();
        topo.setBorderWidth(0);
        topo.setSpacing(0);

//        if (widgetTitulo != null) {
//            topo.add(widgetTitulo);
//        }

        topo.add(containerBarraMenu);
        topo.add(containerTabbar);
        topo.setWidth("100%");

        layout = new DockPanel();
        layout.setSpacing(0);
        layout.setBorderWidth(0);
        layout.setWidth("100%");
        layout.add(topo, DockPanel.NORTH);
        layout.add(conteudo, DockPanel.CENTER);
        layout.add(containerBarraComandos, DockPanel.SOUTH);

        initWidget(layout);

        RootPanel.get("as-carregando").getElement().getParentNode().removeChild(RootPanel.get("as-carregando").getElement());
        RootPanel.get("conteudo").add(this);
    }

    public void exibirStatus() {
        status.setPopupPositionAndShow(new PositionCallback() {

            public void setPosition(int offsetWidth, int offsetHeight) {
                int left = (Window.getClientWidth() - offsetWidth) / 2;
                int top = Window.getScrollTop() + 2;
                status.setPopupPosition(left, top);
            }
        });
    }

    public void ocultarStatus() {
        status.hide();
    }

    public void exibirMenu(Object usuario) {
        containerBarraMenu.setVisible(true);
        containerBarraComandos.setVisible(false);
        containerTabbar.setVisible(true);
        conteudo.clear();
        conteudo.setHeight((Window.getClientHeight() - SCROLL_DIFF) + "px");
        if (containerBarraMenu.getWidget() != null) {
            ((ASMenuBar) containerBarraMenu.getWidget()).setUsuario(usuario);
        }
    }

    public void ocultarMenu(Widget widgetInicial) {
        ((ASMenuBar) containerBarraMenu.getWidget()).setUsuario(null);
        containerBarraMenu.setVisible(false);
        containerBarraComandos.setVisible(false);
        containerTabbar.setVisible(false);
        tabbar.removePaginas();
        conteudo.clear();
        conteudo.add(new LoginPagina(null, ReservaSISApplication.getInstance(), ASOrientation.HORIZONTAL));
    }

    public void addComando(Widget widget) {
        barraComandos.add(widget);
    }

    public void addComandos(List<Widget> widgets) {
        barraComandos.clear();

        for (Widget w : widgets) {
            addComando(w);
        }
    }

    public void removeComandos() {
        barraComandos.clear();
    }

    public void addPagina(ASPage pagina) {
        tabbar.addPagina(pagina);
        containerBarraComandos.setVisible(true);
    }

    @Override
    public void removePagina(ASPage pagina) {
        tabbar.removePagina(pagina);
    }

    public void setMenu(ASMenuBar menu) {
        containerBarraMenu.setWidget(menu);
    }
}
